
estimation_data <-
  estimation_data %>%
  mutate(gender = case_when(V_1 == "Laki-laki" ~ 1,
                            V_1 == "Perempuan" ~ 0,
                            TRUE ~ NA_real_)) %>%
  mutate(age = V_2)  %>%
  mutate(college_diploma = case_when(V_3 == "Sekolah Menengah Atas (SMA) atau sederajat" ~ 0,
                                     V_3 == "Diploma (D1/D2/D3)" ~ 1,
                                     V_3 == "S1" ~ 1,
                                     V_3 == "S2" ~ 1, 
                                     V_3 == "S3" ~ 1,
                                     TRUE ~ NA_real_))  %>%
  group_by(ID) %>%
  mutate(sum_wave = sum(A)) %>%
  ungroup() %>%
  mutate(post_election_attrit = case_when(JML == "Hanya diwawancara sekali" & sum_wave %in% c(2) ~ 1,
                                          JML == "Diwawancara 2 kali (pada survei gelombang I & II atau I & III atau II & III)" & sum_wave == 3 ~ 1,
                                          TRUE ~ 0))

p1 <- lm_robust(gender ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p2 <- lm_robust(age ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p3 <- lm_robust(college_diploma ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p4 <- lm_robust(islam_recode_plot == "Islam" ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p5 <- lm_robust(javanese_recode_plot == "Javanese" ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p6 <- lm_robust(list_position_recode_plot ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p7 <- lm_robust(islam_party_recode_plot == "Islamist" ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)
p8 <- lm_robust(validated_election_outcome == "Elected" ~ post_election_attrit, data = estimation_data, clusters = ID) %>% tidy() %>% filter(term == "post_election_attrit") %>% pull(p.value)

pvals <- c(p1, p2, p3, p4, p5, p6, p7, p8)


dat <-
  estimation_data %>%
  group_by(post_election_attrit) %>%
  summarise(val_gender = mean(gender, na.rm = T),
            val_age = mean(age, na.rm = T),
            val_educ = mean(college_diploma, na.rm = T),
            val_islam = mean(islam_recode_plot == "Islam", na.rm = T),
            val_javanese = mean(javanese_recode_plot == "Javanese", na.rm = T),
            val_list = mean(list_position_recode_plot, na.rm = T),
            val_islamist = mean(islam_party_recode_plot == "Islamist", na.rm = T),
            val_win = mean(validated_election_outcome == "Elected", na.rm = T)) %>%
  t() %>%
  as.data.frame() %>%
  slice(2:nrow(.)) %>%
  rownames_to_column() %>%
  set_colnames(c("var", "Non-Attriter", "Post-Election Attriter")) %>%
  bind_cols(pval = pvals) %>%
  mutate_at(vars(`Non-Attriter`:pval), funs(round(as.numeric(.), digits = 3))) %>%
  mutate(var = case_when(var == "val_age" ~ "Age",
                         var == "val_educ" ~ "Education: (4-pt)",
                         var == "val_gender" ~ "Gender: Man",
                         var == "val_islam" ~ "Religion: Islam",
                         var == "val_javanese" ~ "Ethnicity: Javanese",
                         var == "val_list" ~ "List Position",
                         var == "val_islamist" ~ "Party: Islamist",
                         var == "val_win" ~ "Outcome: Winner")) %>%
  set_colnames(c("Variable", "Non-Attriter", "Post-Election Attriter", "p-value"))


dat %>%
  kable(
    "latex",
    booktabs = TRUE,
    escape = FALSE,
    longtable = TRUE,
    cap = "\\label{tab:demo_attrition}Demographic Traits of Attriters vs. non-Attriters",
    col.names = linebreak(c(
      "Variable", "Non-Attriter", "Post-Election Attriter", "$p$"
    )),
    align = c("l", rep("c", 6)),  # 7 total
    linesep = ""
  ) %>%
  kable_styling(
    font_size = 9,
    latex_options = c("scale_down", "hold_position", "striped")
  ) %>%
  cat(., file = "./outputs/tables/table_a5.tex")


